/*
 * Copyright 2002-2018 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.transaction.support;

import org.springframework.lang.Nullable;
import org.springframework.transaction.TransactionStatus;

/**
 * Callback interface for transactional code. Used with {@link TransactionTemplate}'s
 * {@code execute} method, often as anonymous class within a method implementation.
 *
 * <p>Typically used to assemble various calls to transaction-unaware data access
 * services into a higher-level service method with transaction demarcation. As an
 * alternative, consider the use of declarative transaction demarcation (e.g. through
 * Spring's {@link org.springframework.transaction.annotation.Transactional} annotation).
 *
 * @author Juergen Hoeller
 * @since 17.03.2003
 * @see TransactionTemplate
 * @see CallbackPreferringPlatformTransactionManager
 * @param <T> the result type
 */
/*
1. 总结
TransactionCallback 是Spring事务框架中的回调接口，用于在事务上下文中执行业务逻辑
它是一个函数式接口(@FunctionalInterface)，支持Lambda表达式和方法引用
主要与 TransactionTemplate 和 CallbackPreferringPlatformTransactionManager 配合使用，简化事务管理代码
2. 内部成员说明
doInTransaction(TransactionStatus status) 方法:
由 TransactionTemplate#execute 在事务上下文中调用
不需要关心事务管理细节，但可以通过 TransactionStatus 对象获取或影响当前事务状态
允许返回在事务中创建的结果对象
如果抛出 RuntimeException，会被视为应用异常并强制回滚事务
3. 类关系结构
	TransactionCallback (interface)
	├── TransactionCallbackWithoutResult (abstract class)
	│   └── 各种匿名实现类
	└── 各种具体实现类和Lambda表达式
相关接口区别说明：
	TransactionCallback: 通用的事务回调接口，允许返回结果对象
	TransactionCallbackWithoutResult: TransactionCallback 的抽象实现类，专门用于不需要返回结果的事务操作，返回类型为 void
4. 使用场景
需要在事务边界内执行业务逻辑操作
希望避免手动管理事务的 begin、commit、rollback 过程
结合 TransactionTemplate 进行编程式事务管理
替代声明式事务(@Transactional)的编程方式
需要组合多个无事务感知的数据访问服务调用
5. 实际代码应用示例
@Service
public class UserService {

    @Autowired
    private TransactionTemplate transactionTemplate;

    // 带返回值的事务操作
    public User createUser(String name, String email) {
        return transactionTemplate.execute(new TransactionCallback<User>() {
            @Override
            public User doInTransaction(TransactionStatus status) {
                // 执行业务逻辑
                User user = new User(name, email);
                // 保存用户...
                return user;
            }
        });
    }

    // 使用Lambda表达式简化代码
    public void updateUser(Long userId, String newName) {
        transactionTemplate.execute(status -> {
            // 更新用户信息逻辑
            // 如果抛出RuntimeException，自动回滚
            return null;
        });
    }

    // 不需要返回结果的事务操作
    public void deleteUser(Long userId) {
        transactionTemplate.execute(new TransactionCallbackWithoutResult() {
            @Override
            protected void doInTransactionWithoutResult(TransactionStatus status) {
                // 删除用户逻辑
                // 无需返回结果
            }
        });
    }
}


 */
@FunctionalInterface
public interface TransactionCallback<T> {

	/**
	 * Gets called by {@link TransactionTemplate#execute} within a transactional context.
	 * Does not need to care about transactions itself, although it can retrieve and
	 * influence the status of the current transaction via the given status object,
	 * e.g. setting rollback-only.
	 * <p>Allows for returning a result object created within the transaction, i.e. a
	 * domain object or a collection of domain objects. A RuntimeException thrown by the
	 * callback is treated as application exception that enforces a rollback. Any such
	 * exception will be propagated to the caller of the template, unless there is a
	 * problem rolling back, in which case a TransactionException will be thrown.
	 * @param status associated transaction status
	 * @return a result object, or {@code null}
	 * @see TransactionTemplate#execute
	 * @see CallbackPreferringPlatformTransactionManager#execute
	 */
	@Nullable
	T doInTransaction(TransactionStatus status);

}
